# -*- coding: utf-8 -*-
# @Time    : 2020/7/31
# @File    : SEIR.py
# import json

# import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as spi

# region plot===全===局===配===置
# plt.figure(figsize=[10, 6])
# plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签
# plt.rcParams['axes.unicode_minus'] = False
# plt.xlabel('第 t 天', fontdict={'size': 20})
# plt.ylabel('人数', fontdict={'size': 20})
# plt.xticks(size=20)
# plt.yticks(size=20)
# plt.title('SEIR 模型', fontdict={'size': 20})
# endregion

# region SEIR===模===型===参===数

# 易感者 初值
S0 = 300000
# 潜伏者 初值
E0 = 0
# 感染者 初值
I0 = 1
# 移出者 初值
R0 = 0

# 潜伏者 -> 感染者
alpha = 1 / 14.3
# 感染者 -> 易感者
beta = 0.2
# 潜伏着 -> 易感者
beta2 = 0.25
# 感染者、潜伏者 -> 移出者
gamma = 0.19

# 非感染者 每人天接触的人数
r = 10
# 感染者 每人天接触的人数(接受隔离)
r2 = 2
# 某地区总人数
N = 300000


# endregion


def SEIR(y, t, alpha, beta, gamma, r, N):
	# y : 微分方程组
	S, E, I, R = y
	r2_beta2 = r2 * beta2
	dS_dt = -(r * beta * I * S + r2_beta2 * E * S) / N
	dE_dt = (r * beta * I * S + r2_beta2 * E * S) / N - alpha * E
	dI_dt = alpha * E - gamma * I
	dR_dt = gamma * I
	return [dS_dt, dE_dt, dI_dt, dR_dt]


def fitting():
	time = np.linspace(0, 194, 194)
	res = spi.odeint(SEIR, [S0, E0, I0, R0], time, args=(alpha, beta, gamma, r, N))
	res = np.array(res)
	S, E, I, R = res[:, 0], res[:, 1], res[:, 2], res[:, 3]
	"""
	plt.plot(t, S, label='S(t)')
	plt.plot(t, E, label='E(t)')
	plt.plot(t, R, label='R(t)移出')
	plt.plot(t, I, label='I(t)预测确诊')
	plt.legend(loc="best", fontsize=20)
	"""
	return np.trunc(I).astype(int)


# def get_data():
# 	# j_str = requests.get('https://file1.dxycdn.com/2020/0731/692/9373385319039177243-135.json?t=26602884')
# 	# data = json.loads(j_str.text)['data']
# 	confirmed = []
# 	cured = []
# 	with open('china.json', 'r') as j_str:
# 		data = json.load(j_str)['data']
# 		for e in data:
# 			confirmed.append(e['currentConfirmedCount'])
# 			cured.append(e['curedCount'])
#
# 	return confirmed, cured
# def actual():
# 	# 实际数据，现存确诊，治愈
# 	confr, cured = get_data()
# 	x = np.linspace(1, 194, 194)
# 	plt.plot(x, cured, label='实际治愈拟合')
# 	plt.plot(x, confr, label='实际确诊拟合')

def get_pre(x: 'int>=0'):
	arr = list(fitting())
	return arr[x - 1]


if __name__ == '__main__':
	print("第50天：", get_pre(50))
